<?php
/**
 * V7 checked 1
 * 
 * @author Leon
 *
 */
class data_friend extends data {

	public function __construct () {
		parent::__construct();
	}
	
	/**
	 * 列出我的好友，
	 * 		可以按“给我推荐的次数”和“给他推荐的次数”来筛选
	 * 		默认按我给他推荐的文章次数排序
	 *
	 * @param Int $uid
	 * @param string $type					// 'send' / 'receive' / ''
	 * @param string $social_type_array
	 * @param Int $page					// 默认值为1；
	 * @param Int $limit					// 可以为0
	 * @param bool $is_one_more_read			// 多读一条作为是否有下一页的判断
	 * @return Array
	 */
	public function ls ($uid, $type='all', $social_type_array='all', $page = 1, $limit = 10, $is_one_more_read=false) {
		$uid		= intval($uid);
		$type		= ($type === 'send' or $type === 'receive') ? $type : '';
		$page	= ($page > 1) ? intval($page) : 1; // 默认为1
		$limit		= intval($limit);
		
		if ($limit > 0 and $is_one_more_read) {
			if ($is_one_more_read) {
				$limit = ' limit ' . (($page - 1) * $limit) . ', ' . ($limit+1);		// 当有limit限制，并且要多读一条的时候
			} else {
				$limit = ' limit ' . (($page - 1) * $limit) . ', ' . $limit;
			}
			
		} else {
			$limit = '';
		}
		
		if ($uid) {
			if ($social_type_array === 'all') {
				$social_where = '';
			} elseif (is_array($social_type_array)) {
				if (in_array('weibo', $social_type_array)) {		$social_where[] = ' users.weibo_id		IS NOT NULL ';	}
				if (in_array('renren', $social_type_array)) {		$social_where[] = ' users.renren_id		IS NOT NULL ';	}
				if (in_array('kaixin001', $social_type_array)) {	$social_where[] = ' users.kaixin001_id	IS NOT NULL ';	}
				if (in_array('qqwb', $social_type_array)) {		$social_where[] = ' users.qqwb_id		IS NOT NULL ';	}
				if (is_array($social_where)) {
					$social_where = ' ( ' . implode(' or ', $social_where) . ') and ';
				} else {
					$social_where = ' 0 and ';	// 如果参数不对，那么就没有查询结果
				}
			} else {
				$social_where = ' 0 and ';		// 如果参数不对，那么就没有查询结果
			}
			$execute = $this->db->execute('
					select
						friend.id					AS id,
						friend.user_id				AS user_id,
						friend.friend_id				AS friend_id,
						friend.nickname				AS nickname,
						friend.total_recommends_send	AS total_recommends_send,
						friend.total_recommends_receive	AS total_recommends_receive,
						users.nickname				AS user_nickname,
						users.weibo_id				AS weibo_id,
						users.renren_id				AS renren_id,
						users.kaixin001_id			AS kaixin001_id,
						users.qqwb_id				AS qqwb_id
					from
						friend, users
					where
						friend.user_id 	= "' . $uid . '"	and
						'.$social_where.'
						'.(($type) ? (($type == 'send') ? ' friend.total_recommends_send > 0 ' : ' friend.total_recommends_receive > 0 ') : ' 1 ').' and
						users.id		= friend.friend_id
					order by
						'.(($type == 'receive') ? ' friend.total_recommends_receive DESC, ' : ' friend.total_recommends_send DESC, ').'
						friend.id ASC
					' . $limit . '
			');
			return $execute;
		} else return false;
	}
	/**
	 * 修改用户的昵称
	 *
	 * @param Int $uid        	
	 * @param Int $id        	
	 * @param String $nickname		// 没有转义
	 */
	public function edit ($uid, $id, $nickname) {
		$uid	= intval($uid);
		$id	= intval($id);
		$nickname = format_nickname($nickname);

		if ($uid and $id and $nickname) {
			return $this->db->execute('
					update
						friend
					set
						nickname	= "' . $nickname . '"
					where
						id		= "' . $id . '"
			');
		} else return false;
	}
	/**
	 * 删除好友
	 * 删除时，同时把对方的好友记录也删除了
	 * 删除完成后重新统计文件夹中的好友总数
	 *
	 * @param int $uid        	
	 * @param int $id
	 *        	friend表中的主键id
	 * @return bool
	 */
	public function del ($uid, $id) {
		$uid	= intval($uid);
		$id	= intval($id);
		
		if ($uid and $id) {
			// 检查参数是否合法
			$me = $this->db->execute('select * from friend where id = "' . $id . '" and user_id = "' . $uid . '"');
			if ($me) {
				// 删除“我”的记录
				$del_me = $this->db->execute('delete from friend where id = "' . $id . '" ');
				// 删除“对方”的记录
				$this->db->execute('delete from friend where user_id = "' . $me[0]['friend_id'] . '" and friend_id = "' . $me[0]['user_id'] . '" ');

				return $del_me;
			} else return false;
		} else return false;
	}
	/**
	 * friend_id是否是uid的好友（参数有顺序关系）
	 *
	 * @param int $uid        	
	 * @param int $friend_id        	
	 * @return bool
	 */
	public function is_friend ($uid, $friend_id) {
		$uid		= intval($uid);
		$friend_id	= intval($friend_id);
		
		if ($uid and $friend_id) {
			$execute = $this->db->execute('
					select
						id
					from
						friend
					where
						user_id	= "' . $uid . '"		and
						friend_id	= "' . $friend_id . '"
					limit
						1
			');
			if ($execute[0]['id']) {
				return true;
			} else return false;
		} else return false;
	}
	/**
	 * 列出好友，排除掉已经被推荐过指定文章的
	 * 
	 * @param int $uid
	 * @param int $article_id
	 * @return Array
	 */
	public function ls_without_recommend_article ($uid, $article_id) {
		$uid		= intval($uid);
		$article_id	= intval($article_id);
		
		if ($uid and $article_id) {
			$execute = $this->db->execute('
				select
					*
				from
					friend
				where
					friend.user_id		= "' . $uid . '"	and
					friend.friend_id		NOT IN (
						select receiver_id from recommend_article where article_id = "' . $article_id . '"
					)				and
					friend.friend_id		NOT IN (
						select user_id from save_article where id = "'.$article_id.'"
					)
				order by
					friend.total_recommends_send	DESC,
					friend.id					ASC
			');
			return $execute;
		} else return false;
	}
	/**
	 * 一条好友记录
	 *
	 * @param int $uid        	
	 * @param int $id        	
	 * @return array
	 */
	public function one ($uid, $id) {
		$uid	= intval($uid);
		$id	= intval($id);
		
		if ($uid and $id) {
			$execute = $this->db->execute('
                                select
				*
                                from
                                        friend
                                where
                                        friend.id 		= "' . $id . '"     and
                                        friend.user_id 	= "' . $uid . '"
                        ');
			if ($execute[0]) {
				return $execute[0];
			} else return false;
		} else return false;
	}
	/**
	 * 检查好友id字符串，确认是好友以后，将确认后的id字符串返回
	 * 
	 * @param int $uid
	 * @param String $friends_id_str
	 * @return string
	 */
	public function filter_is_friends ($uid, $friends_id_str) {
		$uid			= intval($uid);
		$friends_id_str	= format_id_string($friends_id_str, false);		// 返回字符串

		if ($uid and $friends_id_str) {
			$execute = $this->db->execute('
				select
					friend_id
				from
					friend
				where
					user_id	= "' . $uid . '" 			and
					friend_id	IN (' . $friends_id_str . ')
			');
			if (is_array($execute)) {
				foreach ( $execute as $v ) {
					$return .= $v['friend_id'] . ',';				// 用“,”分隔开
				}
				$return = substr($return, 0, -1);				// 去掉最后一个“,”
				return $return;							// 返回字符串
			} else return false;
		} else return false;
	}
	/**
	 * 添加好友记录，两条、互相的记录，同时把nickname写进去
	 *
	 * @param int $uid        	
	 * @param int $friend_id        	
	 * @return int
	 */
	public function add ($uid, $friend_id) {
		$uid		= intval($uid);
		$friend_id	= intval($friend_id);
		
		if ($uid and $friend_id and ($uid != $friend_id)) {
			$data_user = new data_user();
			$u	= $data_user->one($uid);
			$f	= $data_user->one($friend_id);
			if ($u and $f) {
				$insert = $this->db->execute('
					insert ignore into
						friend
					(user_id, friend_id, nickname)
						values
					("' . $uid . '", 		"' . $friend_id . '",		"' . $f['nickname'] . '"),
					("' . $friend_id . '", 	"' . $uid . '",			"' . $u['nickname'] . '")
				');
				if ($insert) {
					// EVENT: 互相添加feed
					new data_event('friend_add', array ('user_id'=>$uid,'friend_id'=>$friend_id), 'high');

					return $insert;
				} else return false;
			} else return false;
		} else return false;
	}
}
?>